<template>
  <!-- 添加用户对话框 -->
  <el-dialog title="添加角色" :visible.sync="dialogVisible" @close="close" width="45%">
    
    <!-- 主体 -->
    <el-form ref="form"  :model="addroles" label-width="80px">
      <el-form-item required label="角色名称">
        <el-input v-model="addroles.roleName"></el-input>
      </el-form-item>
      <el-form-item required label="角色描述">
        <el-input v-model="addroles.roleDesc"></el-input>
      </el-form-item>
    </el-form>

    <!-- 底部 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('OutdialogVisible')">取消</el-button>
      <el-button type="primary" @click="BtnPostroles">
        提交
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'AddRoles',
  data() {
    return {
      dialogVisible: false,
      addroles: {
        roleName: '',
        roleDesc: ''
      }
    }
  },
  props: {
    dialog: Boolean,
    addrolesFather: Object
  },
  watch: {
    dialog() {
      this.dialogVisible = this.dialog
      if (this.addrolesFather) {
        this.addroles.roleName = this.addrolesFather.roleName
        this.addroles.roleDesc = this.addrolesFather.roleDesc
      }
    },
  },
  methods: {
    close() {
      this.$emit('OutdialogVisible')
      this.addroles.roleName = '';
      this.addroles.roleDesc = '';
    },
    BtnPostroles() { //按钮提交用户编辑添加

      if (this.addrolesFather) {
        
        this.$http.put(`roles/${this.addrolesFather.id}`, this.addroles).then(res => {
          if (res.data.meta.status !== 200) {
            return this.$mess(res.data.meta.msg, 'error')
          }
          this.$mess('更新成功', 'success');
          this.$emit('getRoles')
        })
      } else {
        this.$http.post('roles', this.addroles).then(res => {
          if (res.data.meta.status !== 201) {
            return this.$mess(res.data.meta.msg, 'error')
          }
          this.$mess(res.data.meta.msg, 'success');
          this.$emit('getRoles')
        })
      }

      this.$emit('OutdialogVisible')
    }
  },
}
</script>

<style>
</style>